﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace winform_buttons_300x300
{
    public partial class UserControl1: UserControl
    {   
        public UserControl1()
        {
            InitializeComponent();
        }

        /*自定义button数量--RowNum ColumNum*/
        
        private void UserControl1_Load(object sender, EventArgs e)
        {

            FillButton();
            LayoutBtns();
 
        }

        int RowNum = 10, ColumNum = 10;
        public void FillButton()
        {
            //填充方格,现在放置的是罗列着的
            for (int i = 0; i < RowNum; i++)
            {
                for (int j = 0; j < ColumNum; j++)
                {
                    Button btn = new Button();
                    this.Controls.Add(btn);
                }
            }

        }
        /*布局设定*/
        private void LayoutBtns()
        {
            //去除启动状态,以免开启的时候FillBtn_SizeChanged会报错
            if (this.Controls.Count == 0)
            {
                return;
            }
            //循环多少次?计算出来
            int btnLineNum = (int)Math.Sqrt(this.Controls.Count);
            //计算按钮的宽度
            int btnWidth = this.Width / ColumNum - 1;
            //计算按钮的高度
            int btnHeight = this.Height / RowNum - 1;

            int btnIndex = 0;
            int btnX = 0, btnY = 0;
            //竖向的循环嵌套横着的循环
            for (int verticalIndex = 0; verticalIndex < RowNum; verticalIndex++)
            {
                btnY = verticalIndex * Height / RowNum;
                //水平向的循环
                for (int horizontalIndex = 0; horizontalIndex < ColumNum; horizontalIndex++)
                {
                    btnX = horizontalIndex * Width / ColumNum;
                    //获取要放置的方格
                    Button btn = this.Controls[btnIndex] as Button;
                    //设置当前方格的大小
                    btn.Size = new Size(btnWidth, btnHeight);
                    //设置当前方格的位置
                    btn.Location = new Point(btnX + 1, btnY + 1);
                    //下一个方格
                    btnIndex++;
                    //把按钮位置（行，列号）绑定到Tag属性中
                    btn.Tag = verticalIndex.ToString() + "," + horizontalIndex.ToString();
                    //绑定click事件                    
                    btn.Click += Btn_Click;
                    btn.Invalidate();
                }
            }
        }

        private void Btn_Click(object sender, EventArgs e)
        {
            MessageBox.Show("OK");
        }

        /*绘制边界*/
        private void UserControl1_Paint_1(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
            g.DrawRectangle(Pens.DarkGray, 0, 0, Width - 1, Height - 1);
            //绘制框线纵轴
            for (int i = 0; i < ColumNum; i++)
            {
                int point_x = Width * i / ColumNum;
                g.DrawLine(Pens.DimGray, point_x, 0, point_x, Height);
            }

            // 绘制框线横轴
            for (int j = 0; j < RowNum; j++)
            {
                int point_y = Height * j / RowNum;
                g.DrawLine(Pens.DimGray, 0, point_y, Width, point_y);
            }
        }
    }
}
